//
//  RLFMDBMaker.m
//  CodeFor链式编程
//
//  Created by relax on 2017/11/16.
//  Copyright © 2017年 relax. All rights reserved.
//

#import "RLFMDBMaker.h"

@interface RLFMDBMaker ()

@property (nonatomic,copy) NSString *columnSQL;
@property (nonatomic,copy) NSString *valuesSQL;
@property (nonatomic,copy) NSString *whereSQL;
@property (nonatomic,copy) NSString *setSQL;

@property (nonatomic,copy) NSString *limitSQL;

@end

@implementation RLFMDBMaker


- (RLFMDBMaker *(^)(NSString *))columnName {
    return ^(NSString *columnName) {
        if (self.columnSQL && ![self.columnSQL isEqualToString:@""]) {
            self.columnSQL = [NSString stringWithFormat:@" %@,%@ ",self.columnSQL,columnName];
        } else {
            self.columnSQL = [NSString stringWithFormat:@" %@ ",columnName];// age
        }

        return self;
    };
}

- (RLFMDBMaker *(^)())integer {
    return ^{
        self.columnSQL = [NSString stringWithFormat:@"%@ INTEGER ", self.columnSQL]; //AGE INTEGER
        return self;
    };
}

- (RLFMDBMaker *(^)())text {
    return ^{
        self.columnSQL = [NSString stringWithFormat:@"%@ TEXT ",self.columnSQL];

        return self;
    };
}


- (RLFMDBMaker *(^)())blob {
    return ^{
        self.columnSQL = [NSString stringWithFormat:@"%@ BLOB ",self.columnSQL];

        return self;
    };
}


- (RLFMDBMaker *(^)())primaryKey {
    return ^{
        self.columnSQL = [NSString stringWithFormat:@"%@ PRIMARY KEY ",self.columnSQL]; // AGE INTEGER PRIMARY KEY
        
        return self;
    };
}

- (RLFMDBMaker *(^)())autoincrement {
    return ^{
        self.columnSQL = [NSString stringWithFormat:@"%@ AUTOINCREMENT ",self.columnSQL];
        return self;
    };
}


- (RLFMDBMaker *(^)())notNull {
    return ^{
        self.columnSQL = [NSString stringWithFormat:@"%@ NOT NULL ",self.columnSQL];
        return self;
    };
}


- (RLFMDBMaker *(^)())create {
    return ^{
        NSMutableString *sql = [NSMutableString stringWithFormat:@"%@",self.sql];//?
        NSRange range = [sql rangeOfString:@"*"];
        [sql replaceCharactersInRange:range withString:self.columnSQL]; // 创建表
        BOOL result = [self.db executeUpdate:sql];

        if (result) {
            NSLog(@"%@",@"数据库创建成功");
        } else {

        }

        return self;
    };
}

// INSERT INTO T_GQS () VALUES (!!!!!)
- (RLFMDBMaker *(^)(id))values {
    return ^(id anObject) {
        if (![anObject isKindOfClass:[NSObject class]]) {
            NSAssert(NO, @"anObject 不是一个对象!");
        } else {
            // if ([anObject isKindOfClass:[NSNumber class]]) {
            if (self.valuesSQL && ![self.valuesSQL isEqualToString:@""]) {
                self.valuesSQL = [NSString stringWithFormat:@"%@,%@",self.valuesSQL,anObject];
            } else {
                self.valuesSQL = [NSString stringWithFormat:@"%@",anObject];
            }
            //} else {

            // }
        }
        
        
        return self;
    };
}

- (RLFMDBMaker *(^)())insert {
    return ^{
        NSMutableString *insertSQL = [NSMutableString stringWithFormat:@"%@",self.sql];
        NSRange range = [insertSQL rangeOfString:@"*"];
        [insertSQL replaceCharactersInRange:range withString:self.columnSQL];
        range = [insertSQL rangeOfString:@"?"];
        [insertSQL replaceCharactersInRange:range withString:self.valuesSQL];

        BOOL result = [self.db executeUpdate:insertSQL];
        if (result) {
            NSLog(@"%@",@"数据插入成功!");
        }

        return self;
    };
}

- (RLFMDBMaker *(^)(NSString *))where {
    return ^(NSString * columnName) {
        if (self.whereSQL && ![self.whereSQL isEqualToString:@"" ]) {
            self.whereSQL = [NSString stringWithFormat:@"%@,%@ ",self.whereSQL,columnName];
        } else {
            self.whereSQL = [NSString stringWithFormat:@"WHERE %@",columnName];
        }

        return self;
    };
}

- (RLFMDBMaker *(^)(id))greateThan {
    return ^(id anObject){

        self.whereSQL = [NSString stringWithFormat:@"%@ > %@ ",self.whereSQL,anObject];

        return self;
    };
}

- (RLFMDBMaker *(^)(id))greateThanOrEqualTo {
    return ^(id anObject) {
        self.whereSQL = [NSString stringWithFormat:@"%@ >= %@ ",self.whereSQL, anObject];
        return self;
    };
}


- (RLFMDBMaker *(^)(id))lessThan {
    return ^(id anObject){
        self.whereSQL = [NSString stringWithFormat:@"%@ < %@ ",self.whereSQL,anObject];
        return self;
    };
}

- (RLFMDBMaker *(^)(id))lessThanOrEqualTo {
    return ^(id anObject) {
        self.whereSQL = [NSString stringWithFormat:@"%@ <= %@ ",self.whereSQL ,anObject];
        return self;
    };
}

- (RLFMDBMaker *(^)(id))equalTo {
    return ^(id anObject) {
        self.whereSQL = [NSString stringWithFormat:@"%@ = %@ ",self.whereSQL,anObject];

        return self;
    };
}

- (RLFMDBMaker *(^)())AND {
    return ^(){
        self.whereSQL = [NSString stringWithFormat:@"%@ AND ",self.whereSQL];
        return self;
    };
}

- (RLFMDBMaker *(^)())OR {
    return ^{
        self.whereSQL = [NSString stringWithFormat:@"%@ OR ",self.whereSQL];
        return self;
    };
}

- (RLFMDBMaker *(^)())select {
    return ^{
        NSMutableString *sql = [NSMutableString stringWithFormat:@"%@",self.sql];
        if (self.columnSQL && ![self.columnSQL isEqualToString:@""]) {
            NSRange range = [sql rangeOfString:@"*"];
            [sql replaceCharactersInRange:range withString:self.columnSQL];
        }

        if (self.whereSQL && ![self.whereSQL isEqualToString:@""]) {
            // 在尾部插入where筛选条件
            [sql insertString:[NSString stringWithFormat:@" %@",self.whereSQL] atIndex:sql.length];
        }

        // 翻页
        if (_limitSQL) {
            [sql insertString:_limitSQL atIndex:sql.length];
        }

        FMResultSet *set = [self.db executeQuery:sql];
        self.resultBlock(set);

        return self;
    };
}

- (RLFMDBMaker *(^)())Delete {
    return ^{
        NSString *sql;
        if (self.whereSQL && ![self.whereSQL isEqualToString:@""]) {
            sql = [NSString stringWithFormat:@"%@ %@",self.sql,self.whereSQL];
        } else {
            sql = [NSString stringWithFormat:@"%@",self.sql];
        }

        BOOL result = [self.db executeUpdate:sql];
        if (result) {
            NSLog(@"%@",@"删除成功!");
        } else {
            NSLog(@"%@",@"数据删除失败!");
            // NSLog(@"%zd",[self.db lastErrorCode]);
        }

        return self;
    };
}


- (RLFMDBMaker *(^)(NSString *))set {
    return ^(NSString *colunmName) {
        self.setSQL = [NSString stringWithFormat:@"SET %@ ",colunmName];
        return self;
    };
}

- (RLFMDBMaker *(^)(id))assignment {
    return ^(id anObject) {
        self.setSQL = [NSString stringWithFormat:@"%@ = %@ ",self.setSQL,anObject];
        return self;
    };
}

- (RLFMDBMaker *(^)())update {
    return ^{
        NSString *updateSQL;
        if (self.setSQL && ![self.setSQL isEqualToString:@""]) {
            updateSQL = [NSString stringWithFormat:@"%@ %@ %@;",self.sql,self.setSQL,self.whereSQL];
        } else {
            // 修改语句
            NSAssert(NO, @"修改语句不能缺少set");
        }

        [self.db open];

        BOOL result = [self.db executeUpdate:updateSQL];
        if (result) {
            NSLog(@"%@",@"修改语句执行成功!");
        } else {
            NSLog(@"%@",@"修改语句执行失败!");
        }

        return self;
    };
}

- (void (^)(NSInteger, NSInteger))limit {
    return ^(NSInteger pageIndex,NSInteger pageCount) {
        _limitSQL = [NSString stringWithFormat:@" limit %zd,%zd",pageIndex,pageCount];
    };
}

@end
